<?php /* УТФ-8:fix */

class SPIcontroller__content__front extends SPIcontroller__content
{
    public function get_last_content( $type, $sort = 'pubdate-', $limit = 3 )
    {
        $r = array();
        $r['block_id'] = $type;
		
		$only_production = $type == 'catalog';

        $qres = SPIm::I('content')->front->select( $type, $sort, $limit );

        while( $item = $qres->fetch() )
        {
            $item['preview'] = SPIm::I('media')->get_public_sourceByIndexOld(
                $this->MEDIA_GID, $item['id'], 1,
                array( 'mode' => 'preview' )
            );
			
            $item['link'] = SPIc::I('front')->link->content( $item['type'], null, $item['id'], $item['name'] );

            $r['items'][] = $item;
        }
        $qres->close();

        $r['link_topic'] = SPIc::I('front')->link->content( $type );
        $r['title']      = SPI::I('lang')->content->typename[$type];

        return $r;
    }

    public function exec( & $content )
    {
        $r = array();

        $content['active_topic'] = $content['env_topic'];
		$isNeedBottomBlock = false;

        switch( $content['env_topic'] )
        {
            case 'news':
                $select_type = 'news';
                $orderby = 'pubdate-';
                $items_per_page = 10;
                break;

            case 'product':
                $select_type = 'catalog';
                $orderby = 'order_index';
                $items_per_page = 10;
                break;

            case 'gallery':
                $select_type = 'gallery';
                $orderby = 't_created-';
                $items_per_page = 10;
                break;
				
            case 'partners':
                $select_type = 'partners';
                $orderby = 'order_index';
                $items_per_page = 10;
                break;

            default:
                return false;
        }
		
		$content['linkway'][] = array(
			'name'                => SPI::I('lang')->content->typename[$content['env_topic']],
			'link'                => SPIc::I('front')->link->page( $content['env_topic'] ),
		);

        //-----------------------------------------------------------------------------------------
        // Details list

        if( $content['env_vars']['i'] )
        {
			if( $select_type == 'news' )
				$content['topic_template_filename'] = 'content/details-news.tpl';
			else
				$content['topic_template_filename'] = 'content/details.tpl';
			//$isNeedBottomBlock = true;
			
            $r['details'] = SPIm::I('content')->front->get_by_id( $content['env_vars']['i'] );
			
			$content['title'] = $r['details']['brand']." ".$r['details']['name'];
			
			if( $select_type == 'catalog' )
			{
				$content['linkway'] = array_merge( $content['linkway'], SPIc::I('catalogs')->front->get_fuul_way_catalog( $r['details']['pid_catalog'] ) );
			}
			
			$content['linkway'][] = array(
				'name'                => $r['details']['name'],
				'anons'     		  => $r['details']['anons'],
				'description'         => $r['details']['description'],
				'link'                => SPIc::I('front')->link->page( $select_type ),
			);
			
            $r['details']['preview'] = SPIm::I('media')->get_public_sourceByIndexNewJpeg(
                $this->MEDIA_GID, $r['details']['id'], 1,
                array( 'mode' => 'preview' )
            );

            $r['details']['photos'] = array();
			$photos = SPIm::I('media')->select( $this->MEDIA_GID_ADD, $content['env_vars']['i'] );
			while( $photosItem = $photos->fetch() )
			{
				$r['details']['photos'][] = SPIm::I('media')->get_public_source( $photosItem, array( 'mode' => 'preview' ) );
			}
			
			$r['details']['params'] = SPIm::I('content_params')->front->selectByContentId( $r['details']['id'] );
			
            if( $r['details']['pubdate'] )
            {
                $r['details']['date'] = strftime( '%d.%m.%Y', strtotime( $r['details']['pubdate'] ) );
            }
        }
        else
        {
            // Recount for pager
            $offset = 0;
			
			$content['topic_template_filename'] = 'content/list.tpl';
			
			$catId = (int)$content['env_vars']['cat'];
			$select_types = is_array( $select_type ) ? $select_type : array( $select_type );
			
			if( in_array( 'catalog', $select_types ) === true && $catId )
			{
				$catalogIds = array( $catId );
				SPIc::I('catalogs')->front->buildCatalogsTree( & $catalogIds, null, $catId, true );
				
				$filterList = $content['env_vars']['filter'] ? explode( '-', $content['env_vars']['filter'] ) : null;
				
				$content['linkway'] = array_merge( $content['linkway'], SPIc::I('catalogs')->front->get_fuul_way_catalog( $catId ) );
				
				$content['contentFilters'] = SPIc::I('filters')->front->getFiltersListForCatalogId( $catId, $filterList );
				
				$content['catalogDescription'] = SPIc::I('catalogs')->front->getCatalogData( $catId );
			}
			
			//-----------------------------------------------------------------------------------------
			// Content list


			foreach( $select_types as $select_type )
			{
				
				if( ! $catId )
				{
					$catId = $select_type == 'catalog' ? null : false;
				}
				
				$content['title'] = SPI::I('lang')->content->typename[$select_type];
				
				$items_count = SPIm::I('content')->front->selectCount( $select_type, $catalogIds, $filterList );
				$page_index = max( 1, $content['env_vars']['page'] );
				$offset = ($page_index-1)*$items_per_page;
				
				$qres = SPIm::I('content')->front->select( $select_type, $orderby, $items_per_page, $catalogIds, $offset, $filterList );

				while( $item = $qres->fetch() )
				{
					$item['type'] = $item['type'] == 'catalog' ? 'product' : $item['type'];
					$item['preview'] = SPIm::I('media')->get_public_sourceByIndexNewJpeg(
						$this->MEDIA_GID, $item['id'], 1,
						array( 'mode' => 'preview' )
					);
					
					$item['name'] = $item['brand']." ".$item['name'];

					if( $item['pubdate'] )
					{
						$item['date'] = strftime( '%d.%m.%Y', strtotime( $item['pubdate'] ) );
					}

					$item['link'] = SPIc::I('front')->link->content( $item['type'], $item['id'], $catId, null, $item['name'] );

					$r['items'][] = $item;
				}
				$qres->close();
				
				/******************* pager ******************/
				
				$pages_count = ceil( $items_count / $items_per_page );

				if( $pages_count > 1 )
				{
					$content['pager'] = array();

					if( $page_index != 1 )
					{
						$content['pager']['prev']['link'] = SPIc::I('front')->link->content( $content['env_topic'], null, $catId, $page_index-1 );
					}

					if( $page_index != $pages_count )
					{
						$content['pager']['next']['link'] = SPIc::I('front')->link->content( $content['env_topic'], null, $catId, $page_index+1 );
					}

					$pages_indexes = SPIc::I('front')->build_pages_indexes( $page_index, $pages_count );
					
					foreach( $pages_indexes as $page_i => $page_value )
					{
						if( $page_value == '..' )
						{
							$content['pager']['pages'][] = '..';
							continue;
						}

						$content['pager']['pages'][] = array(
							'index'     => $page_i,
							'link'      => SPIc::I('front')->link->content( $content['env_topic'], null, $catId, $page_i ),
							'is_active' => $page_i == $page_index,
						);
					}
				}
				
			}
		}
		
		$content['activeCatalogId'] = $catId;
		
		if( $isNeedBottomBlock )
		{
			$r['bottom']           = SPIc::I('content')->front->get_last_content( $select_type, 'random' );
			$r['bottom']['link']   = false;
			$r['bottom']['title']  = SPI::I('lang')->content->see_too;
		}
        $content['data'] = $r;
        return true;
    }
	
	public function build_linkway( $item_id = null )
	{
		$linkway = array();

		while( $item_id )
		{
			$linkway_item = SPIm::I('catalogs')->front->get_by_id( $item_id );
			$linkway_item['link'] = SPIc::I('front')->link->page( $linkway_item['type'] );

			$linkway[] = $linkway_item;
			
			$item_id = $linkway_item['parrent_id'];
		}		
		
		return array_reverse( $linkway );
	}
}
